package com.yunxitest.yunxitest.item.hdmi

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.graphics.SurfaceTexture
import android.hardware.Camera
import android.os.Handler
import android.os.HandlerThread
import android.os.Looper
import android.view.TextureView
import com.yunxitest.yunxitest.Interface.IDetectItem
import com.yunxitest.yunxitest.Interface.IResultCallback
import com.yunxitest.yunxitest.R
import kotlinx.android.synthetic.main.activity_main.*


/**
 * Created by apple on 2018/10/19.
 */
class Hdmi2Camera(var mContext: Context,var inResultCallback: IResultCallback?) : IDetectItem {



    /**
     * An additional thread for running tasks that shouldn't block the UI.
     */
    private var mBackgroundThread: HandlerThread? = null

    /**
     * A [Handler] for running tasks in the background.
     */
    private var mBackgroundHandler: Handler? = null

    var TAG = "Hdmi2Camera"

    private val previewWidth = 1280

    private val previewHeight = 720

    private var mCamera: Camera? = null


    var texture_view2: TextureView? = null

    var isSuccess:Boolean?=false

    override fun startDetect() {
        start()
        isSuccess=false
        Handler(Looper.getMainLooper()).postDelayed({
            if(!isSuccess!!){
                inResultCallback?.fail("Hdmi2Camera time out")
            }
        },3000)
    }

    override fun finish() {

    }
    private fun start() {
        texture_view2 = (mContext as Activity).findViewById(R.id.texture_view2)//在获取
        mBackgroundThread = HandlerThread("CameraBackground")
        mBackgroundThread?.start()
        mBackgroundHandler = Handler(mBackgroundThread?.getLooper())

        if (texture_view2?.isAvailable!!) {
            doOpenCamera(texture_view2!!.surfaceTexture)
        } else {
            texture_view2?.surfaceTextureListener = mSurfaceTextureListener
        }


    }

    /**
     * [texture_view2.SurfaceTextureListener] handles several lifecycle events on a
     * [texture_view2].
     */
    private val mSurfaceTextureListener = object : TextureView.SurfaceTextureListener {

        override fun onSurfaceTextureAvailable(surfaceTexture: SurfaceTexture,
                                               width: Int, height: Int) {
//            encoderRunnable = VideoEncoderRunnable()
//
//            Thread(encoderRunnable).start()
            doOpenCamera(texture_view2!!.surfaceTexture)
        }

        override fun onSurfaceTextureSizeChanged(surfaceTexture: SurfaceTexture,
                                                 width: Int, height: Int) {
        }

        override fun onSurfaceTextureDestroyed(surfaceTexture: SurfaceTexture): Boolean {
            return true
        }

        override fun onSurfaceTextureUpdated(surfaceTexture: SurfaceTexture) {

        }
    }

    @SuppressLint("MissingPermission")
    private fun doOpenCamera(surfaceTexture: SurfaceTexture) {
        mCamera = Camera.open(1)
        if (mCamera == null) {
            return;
        }
        mCamera?.setDisplayOrientation(90)

        mCamera?.setPreviewTexture(surfaceTexture)

        var myParam: Camera.Parameters = mCamera?.parameters!!


        // 设置预浏尺寸，注意要在摄像头支持的范围内选择
        myParam.setPreviewSize(previewWidth, previewHeight)
        mCamera?.parameters = myParam
        //mCamera.setPreviewCallback(this)
        mCamera?.setPreviewCallback { data, camera ->
            (mContext as Activity).runOnUiThread({
                inResultCallback?.success()
                isSuccess=true
                inResultCallback=null
            })
        }
        mCamera?.startPreview()



    }


}
